﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OracleCompare
{
    public class Configs
    {
        /// <summary>
        /// 源数据库
        /// </summary>
        public static string SourceConnection { get; set; }

        /// <summary>
        /// 目标数据库
        /// </summary>
        public static string TargetConnection { get; set; }

        /// <summary>
        /// 源数据库字段
        /// </summary>
        public static List<TableCol> SourceTable { get; set; }

        /// <summary>
        /// 目标数据库字段
        /// </summary>
        public static List<TableCol> TargetTable { get; set; }

        /// <summary>
        /// 数据结构相同的表名
        /// </summary>
        public static List<string> SameTable { get; set; }

        /// <summary>
        /// 不同的记录
        /// </summary>
        public static List<Dictionary<string, string>> DifferentList { get; set; }

        /// <summary>
        /// 只在源的记录
        /// </summary>
        public static List<Dictionary<string, string>> SourceList { get; set; }

        /// <summary>
        /// 只在目标的记录
        /// </summary>
        public static List<Dictionary<string, string>> TargetList { get; set; }

        /// <summary>
        /// 相同的记录
        /// </summary>
        public static List<Dictionary<string, string>> SameList { get; set; }

        /// <summary>
        /// 源表全数据
        /// </summary>
        public static List<Dictionary<string, string>> SourceAll { get; set; }

        /// <summary>
        /// 目标表全数据
        /// </summary>
        public static List<Dictionary<string, string>> TargetAll { get; set; }

        public static bool IsSameTableCol(TableCol x, TableCol y)
        {
            if (null == x && null != y) return false;
            if (null != x && null == y) return false;
            if (x.TableName != y.TableName) return false;
            if (x.ColumnName != y.ColumnName) return false;
            if (x.DataType != y.DataType) return false;
            if (x.DataLength != y.DataLength) return false;
            if (x.DataPrecision != y.DataPrecision) return false;
            if (x.DataScale != y.DataScale) return false;
            if (x.IsKey != y.IsKey) return false;
            if (x.Nullable != y.Nullable) return false;
            if (x.Unique != y.Unique) return false;
            if (x.DataDefault != y.DataDefault) return false;
            if (x.Columndes != y.Columndes) return false;
            if (x.Index != y.Index) return false;
            if (x.ForeignKey == null && y.ForeignKey != null) return false;
            if (x.ForeignKey != null && y.ForeignKey == null) return false;
            if (x.ForeignKey != null && y.ForeignKey != null)
            {
                if (x.ForeignKey.TableName != y.ForeignKey.TableName) return false;
                if (x.ForeignKey.ColumnName != y.ForeignKey.ColumnName) return false;
                //外键删除时
                if (x.ForeignKey.DataDefault != y.ForeignKey.DataDefault) return false;
                //外键启用状态
                if (x.ForeignKey.Nullable != y.ForeignKey.Nullable) return false;
            }
            return true;
        }
    }

    public class TableCol
    {
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 字段名
        /// </summary>
        public string ColumnName { get; set; }

        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; set; }

        /// <summary>
        /// 数据长度
        /// </summary>
        public uint DataLength { get; set; }

        /// <summary>
        /// 精度
        /// </summary>
        public uint? DataPrecision { get; set; }

        /// <summary>
        /// 比例
        /// </summary>
        public int? DataScale { get; set; }

        /// <summary>
        /// 是否主键
        /// </summary>
        public bool IsKey { get; set; }

        /// <summary>
        /// 是否允许空
        /// </summary>
        public bool Nullable { get; set; }

        /// <summary>
        /// 是否唯一键
        /// </summary>
        public bool? Unique { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public string DataDefault { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Columndes { get; set; }

        /// <summary>
        /// 外键
        /// </summary>
        public TableCol ForeignKey { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public uint Index { get; set; }

        /// <summary>
        /// 检查
        /// </summary>
        public string Check { get; set; }
    }

    //public class TableColComparer : EqualityComparer<TableCol>
    //{
    //    public override bool Equals(TableCol x, TableCol y)
    //    {
    //        if (null == x && null != y) return false;
    //        if (null != x && null == y) return false;
    //        if (x.TableName != y.TableName) return false;
    //        if (x.ColumnName != y.ColumnName) return false;
    //        if (x.DataType != y.DataType) return false;
    //        if (x.DataLength != y.DataLength) return false;
    //        if (x.DataPrecision != y.DataPrecision) return false;
    //        if (x.DataScale != y.DataScale) return false;
    //        if (x.IsKey != y.IsKey) return false;
    //        if (x.Nullable != y.Nullable) return false;
    //        if (x.DataDefault != y.DataDefault) return false;
    //        if (x.Columndes != y.Columndes) return false;
    //        if (x.Index != y.Index) return false;
    //        return true;
    //    }
    //    public override int GetHashCode(TableCol obj)
    //    {
    //        var hcode = obj.TableName + ":" + obj.ColumnName;
    //        return hcode.GetHashCode();
    //    }
    //}

    public enum TableType
    {
        Different = 0,
        Source = 1,
        Target = 2,
        Same = 3
    }
}
